package curator;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

public class CuratorMain {
    public static void main(String[] args) throws Exception {
        CuratorFramework curatorClient = CuratorFrameworkFactory.newClient("localhost:2181", new ExponentialBackoffRetry(1000, 3));
        curatorClient.start();
        //创建节点
        curatorClient.create().forPath("/test");
        curatorClient.create().forPath("/test","test".getBytes());
        //递归创建节点
        String s = curatorClient.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/test/t");

        //删除节点
        Void unused = curatorClient.delete().forPath("/test");
        //递归删除
        curatorClient.delete().deletingChildrenIfNeeded().forPath("/test");
        //删除指定版本
        curatorClient.delete().withVersion(1).forPath("/test");

        //获取数据
        byte[] bytes = curatorClient.getData().forPath("/test");
        byte[] bytes1 = curatorClient.getData().storingStatIn(new Stat()).forPath("/test");
        //更新数据
        Stat aa = curatorClient.setData().forPath("/test", "aa".getBytes());
        curatorClient.setData().withVersion(1).forPath("/test", "aa".getBytes());

    }
}

